<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./lib/bootstrap.css">
    <script src="./lib/jquery.js"></script>
</head>

<body style="padding: 15px;">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h3 class="panel-title">添加新图书</h3>
        </div>
        <div class="panel-body form-inline">

            <div class="input-group">
                <div class="input-group-addon">书名</div>
                <input type="text" class="form-control" id="iptBookname" placeholder="请输入书名">
            </div>
            <div class="input-group">
                <div class="input-group-addon">作者</div>
                <input type="text" class="form-control" id="iptAuthor" placeholder="请输入作者">
            </div>
            <div class="input-group">
                <div class="input-group-addon">出版社</div>
                <input type="text" class="form-control" id="iptPublisher" placeholder="请输入出版社">
            </div>

            <button id="btnAdd" class="btn btn-primary">添加</button>
            <button id="btnAdd1" class="btn btn-primary">查找</button>


        </div>
    </div>
    <!-- 图书的表格 -->
    <table class="table table-bordered table-hover">
        <thead>
            <tr>
                <th>Id</th>
                <th>书名</th>
                <th>作者</th>
                <th>出版社</th>
                <th>操作</th>
            </tr>

        </thead>
        <tbody id="tb"></tbody>
    </table>
    <div class="xx"></div>
    <script>

        //渲染
        function getBooklist() {
            $.get('http://www.liulongbin.top:3006/api/getbooks', res => {
                const { status, data, msg } = res
                if (status != 200) return alert(msg)
                let arr = []
                data.forEach(item => {
                    arr.push(
                        `
                        <tr>
               <td>${item.id}</td>
               <td>${item.bookname}</td>
               <td>${item.author}</td>
               <td>${item.publisher}</td>
               <td>
                <a href="javascript:;" class="del " data-id="${item.id}">删除</a>
                </td>
            </tr>
                        `
                    )
                })
                $('#tb').html(arr.join(''))
            })

        }
        getBooklist()

        //添加
        $('#btnAdd').on('click', function () {
            const bookname = $('#iptBookname').val().trim()
            const author = $('#iptAuthor').val().trim()
            const publisher = $('#iptPublisher').val().trim()

            if (bookname.length === 0 || author.length === 0 || publisher.length === 0) return alert('错误')
            $.post('http://www.liulongbin.top:3006/api/addbook', { bookname, author, publisher }, res => {
                const { status, msg } = res
                if (status != 201) return alert(msg)
                getBooklist()

                $('#iptBookname').val('')
                $('#iptAuthor').val('')
                $('#iptPublisher').val('')
            })
        })

        //删除
        $('#tb').on('click', '.del', function () {
            if (window.confirm('你要删除吗')) {
                const id = $(this).attr('data-id')
                $.get('http://www.liulongbin.top:3006/api/delbook', { id }, res => {
                    const { status, msg } = res
                    if (status !== 200) return alert(msg)
                    getBooklist()
                })
            }
        })

        //查找
        $('#btnAdd1').on('click', function () {
            const bookname = $('#iptBookname').val().trim()
            if(bookname.length ===0) return alert("sb")
            $.get('http://www.liulongbin.top:3006/api/getbooks',{bookname},res=>{
                if(res.data.length === 0){
                    alert('找不到')
                }else{
                    $('.xx').html(`书名:${res.data[0].bookname} 作者:${res.data[0].author} 出版社:${res.data[0].publisher}`)
                }
            })
        })
    </script>
</body>

</html>